package com.douqu.game.core.entity.ext;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.InstanceMap;
import com.douqu.game.core.entity.Level;
import com.douqu.game.core.entity.WorldInfo;
import com.douqu.game.core.entity.sprite.Player;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.util.JsonUtils;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* @author wangzhenfei
*         2017-08-08 16:03
*         玩家副本信息
*  地图：
*      地图id
*      关卡list
*      已经获取的地图奖励
*   关卡：
*      关卡id
*      已获得最高星星数
*      是否是第一次过关
*/
public class InstanceInfo extends ExtInfo{
    /**
     * 已经完成的地图列表
     */
    private List<InstanceMap> instanceList;
    private Map<Integer, InstanceMap> instanceMap;

    /**
     * 可以进行的地图列表
     */
    private List<Integer> canPassMap;

    /**
     * 可以进行的地关卡列表
     */
    private List<Integer> nextPassLevel;

    public List<InstanceMap> getInstanceList() {
        return instanceList == null? instanceList = new ArrayList<InstanceMap>() : instanceList;
    }

    public void setInstanceList(List<InstanceMap> instanceList) {
        this.instanceList = instanceList;
    }

    public Map<Integer, InstanceMap> getInstanceMap() {
        return instanceMap;
    }

    public void setInstanceMap(Map<Integer, InstanceMap> instanceMap) {
        this.instanceMap = instanceMap;
    }

    public List<Integer> getNextPassLevel() {
        return nextPassLevel == null? nextPassLevel = new ArrayList<>() : nextPassLevel;
    }

    public void setNextPassLevel(List<Integer> nextPassLevel) {
        this.nextPassLevel = nextPassLevel;
    }

    public List<Integer> getCanPassMap() {
        return canPassMap == null? canPassMap = new ArrayList<Integer>() : canPassMap;
    }

    public void setCanPassMap(List<Integer> canPassMap) {
        this.canPassMap = canPassMap;
    }

    public InstanceInfo(Player player, WorldInfo worldInfo) {
        super(player, worldInfo);
        reset();
    }

    public void addInstanceMap(InstanceMap map){
        if(map != null && !instanceMap.containsKey(map.getId())){
             instanceList.add(map);
             instanceMap.put(map.getId(), map);

        }
    }




    private void reset() {
        canPassMap = new CopyOnWriteArrayList<>();
        instanceList = new CopyOnWriteArrayList<>();
        instanceMap =  new ConcurrentHashMap();
    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        System.out.println("writeTo buffer:"+buffer.length());
        buffer.writeByte(instanceList.size());
        for(InstanceMap map : instanceList)
        {
            buffer.writeInt(map.getId());
            buffer.writeByte(map.getFinishLevels().size());
            for(Level l : map.getFinishLevels()){
                buffer.writeInt(l.getMaxStars());
                buffer.writeInt(l.getId());
                buffer.writeBoolean(l.isReceive());

            }
            buffer.writeByte(map.getHasReward().size());
            for(Integer rewardId : map.getHasReward()){
                buffer.writeInt(rewardId);
            }
        }
        buffer.writeByte(canPassMap.size());
        for(Integer id : canPassMap){
            buffer.writeInt(id);
        }

        buffer.writeByte(getNextPassLevel().size());
        for(Integer nextId : getNextPassLevel()){
            buffer.writeInt(nextId);
        }
        System.out.println("writeTo buffer:"+buffer.length());

        buffer.writeInt(0);//备用
    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        reset();
        System.out.println("loadFrom buffer:"+buffer.length());
        try {
            int instanceMapSize = buffer.readByte();
            InstanceMap insMap;
            Level level;
            for(int i = 0 ; i < instanceMapSize; i++){
                insMap = new InstanceMap();
                insMap.setId(buffer.readInt());
                int levelSize = buffer.readByte();
                for(int j = 0 ; j < levelSize; j++) {
                    int maxStar = buffer.readInt();
                    int id = buffer.readInt();
                    boolean receive = buffer.readBoolean();
                    level = DataFactory.getInstance().getGameObject(DataFactory.LEVEL_KEY, id);
                    Level newLevel = (Level) Level.cloneObject(level);
                    newLevel.setMaxStars(maxStar);
                    newLevel.setReceive(receive);

                    insMap.addLevel(newLevel);
                }

                int rewardSize = buffer.readByte();
                for(int j = 0; j < rewardSize; j ++){
                    insMap.getHasReward().add(buffer.readInt());
                }

                addInstanceMap(insMap);
            }
            int canPassMapSize = buffer.readByte();
            for(int i = 0; i < canPassMapSize; i++){
                getCanPassMap().add(buffer.readInt());
            }

            int nextLevelSize = buffer.readByte();
            for(int k = 0 ; k < nextLevelSize; k++){
                getNextPassLevel().add(buffer.readInt());
            }

            if(canPassMap.size() == 0){
                changeNext();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("loadFrom buffer:"+buffer.length());


    }

    @Override
    public void init() {
        changeNext();
    }

    public void changeNext(){
        List<InstanceMap> list = DataFactory.getInstance().getDataList(DataFactory.INSTANCE_KEY);
        Integer canPassMapId = canPassMap.size() == 0? 0 : canPassMap.get(canPassMap.size() - 1);
        if(canPassMapId == 0){ // 第一次
            //初始化第一张图的第一个关卡
            if(list.size() > 0){
                InstanceMap map = list.get(0);
                canPassMap.add(map.getId());
                InstanceMap instanceMap = new InstanceMap();
                instanceMap.setId(map.getId());
                addInstanceMap(instanceMap);
                if(map.levels.length > 0){
                    getNextPassLevel().add(map.levels[0]);
                }

            }
        }else {
            InstanceMap currentMap = null;
            for(InstanceMap map : list){
                if(map.getId() == canPassMapId){
                    currentMap = map;
                    break;
                }
            }

            if(currentMap != null){
                //当前地图是否都过关了
                if(instanceMap.get(currentMap.getId()).getFinishLevels().size() == currentMap.levels.length){
                    //开启下张地图
                    canPassMapId = currentMap.unLockInstance;
                    canPassMap.add(currentMap.unLockInstance);
                    if(currentMap.unLockInstance != 0 ){

                        for(InstanceMap map : list){
                            if(map.getId() == canPassMapId){
                                currentMap = map;
                                break;
                            }
                        }
                        if(currentMap != null){
                            nextPassLevel.clear();
                            nextPassLevel.add(currentMap.levels[0]);
                        }

                    }
                }else{
                    //只增加关卡
                    int index = instanceMap.get(currentMap.getId()).getFinishLevels().size();
                    nextPassLevel.clear();
                    nextPassLevel.add(currentMap.levels[index]);
                }
            }
        }

        System.out.println("\n改变可通过关卡信息：\ncanPassMap : " + canPassMap + "\nnextLevel : " + nextPassLevel);

    }

    @Override
    public String toString() {
        return "副本打印信息：canPassMap : " + canPassMap + "\n nextLevel : " + nextPassLevel +
                "\n  finishInstanceList :" + instanceList;
    }
}
